package com.mmx.task;

import com.mmx.constant.NodeConstant;
import com.mmx.domain.bean.FlowData;
import com.mmx.domain.bean.LineData;
import com.mmx.domain.bean.NodeData;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @desc: 任务处理抽象类
 * @author: liushuai
 * @date: 2023/1/31 15:15
 */
public abstract class AbstractTaskNode {

    public static final List<String> TRANSIT_NODE = new ArrayList<>();

    static {
        TRANSIT_NODE.add(NodeConstant.EXCLUSIVE_GATEWAY);
        TRANSIT_NODE.add(NodeConstant.INCLUSIVE_GATEWAY);
        TRANSIT_NODE.add(NodeConstant.PARALLEL_GATEWAY);
    }


    /**
     * 流程流转执行方法
     * @param flowObj
     * @param currentNode
     * @param variables
     * @return: java.util.List<com.mmx.domain.bean.NodeData>
     * @Author: liushuai
     * @Date: 2023/1/31 17:26
     */
    public abstract List<NodeData> execute(FlowData flowObj, NodeData currentNode, Map<String, Object> variables);

    protected List<NodeData> getTargerNode(FlowData flowObj,NodeData currentNode){
        List<LineData> edges = flowObj.getEdges();
        List<NodeData> nodeDatas = new ArrayList();
        List<LineData> targetLines = new ArrayList();
        for(LineData lineData: edges) {
            if(currentNode.getId().equals(lineData.getSourceNodeId())){
                targetLines.add(lineData);
            }
        }
        List<NodeData> nodes = flowObj.getNodes();
        for(NodeData nodeData: nodes) {
            for(LineData lineData: targetLines) {
                if(lineData.getTargetNodeId().equals(nodeData.getId())){
                    nodeData.setCondition(lineData.getProperties().getCondition());
                    nodeDatas.add(nodeData);
                }
            }

        }
        return  nodeDatas;
    }

}
